<?php

function existsAddress($BDD_connexion, $method = "POST") {
	
	$rueMaxLength = 255;
	$villeMaxLength = 100;
	$paysMaxLength = 100;
	
	$request = $BDD_connexion->prepare("
		SELECT id_adresse FROM adresse WHERE numero = :numero AND
											 rue_1 = :rue_1 AND
											 rue_2 = :rue_2 AND
											 rue_3 = :rue_3 AND
											 cp = :cp AND
											 ville = :ville AND
											 pays = :pays");
	$request->bindValue(':numero', $_POST["numero"], PDO::PARAM_INT);
	$request->bindParam(':rue_1', htmlentities($_POST["rue_1"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $rueMaxLength);
	$request->bindParam(':rue_2', htmlentities($_POST["rue_2"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $rueMaxLength);
	$request->bindParam(':rue_3', htmlentities($_POST["rue_3"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $rueMaxLength);
	$request->bindValue(':cp', $_POST["cp"], PDO::PARAM_INT);
	$request->bindParam(':ville', htmlentities($_POST["ville"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $villeMaxLength);
	$request->bindParam(':pays', htmlentities($_POST["pays"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $paysMaxLength);
	$request->execute();
	
	$idAddress = $request->fetch(PDO::FETCH_OBJ);
	
	if (isset($idAddress->id_adresse))
	{
		return $idAddress->id_adresse;
	}
	else
	{
		return 0;
	}
	
}

function addAddress($BDD_connexion, $method = "POST") {
	
	$rueMaxLength = 255;
	$villeMaxLength = 100;
	$paysMaxLength = 100;
	
	//ajouter adresse
	$request = $BDD_connexion->prepare("
		INSERT INTO adresse (numero,rue_1,rue_2,rue_3,cp,ville,pays)
		VALUES (:numero,:rue_1,:rue_2,:rue_3,:cp,:ville,:pays);");
	$request->bindValue(':numero', $_POST["numero"], PDO::PARAM_INT);
	$request->bindParam(':rue_1', htmlentities($_POST["rue_1"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $rueMaxLength);
	$request->bindParam(':rue_2', htmlentities($_POST["rue_2"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $rueMaxLength);
	$request->bindParam(':rue_3', htmlentities($_POST["rue_3"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $rueMaxLength);
	$request->bindValue(':cp', $_POST["cp"], PDO::PARAM_INT);
	$request->bindParam(':ville', htmlentities($_POST["ville"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $villeMaxLength);
	$request->bindParam(':pays', htmlentities($_POST["pays"],ENT_NOQUOTES,"UTF-8"), PDO::PARAM_STR, $paysMaxLength);
	$request->execute();
	
	return $BDD_connexion->lastInsertId();
	
}

?>
